package com.s7_compat.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.s7.compat.utils.DateUtils;
import com.s7_compat.R;
import com.s7_compat.service.HintService;

public class ServiceActivity extends AppCompatActivity {

    HintService.DeviceIBinder binder;
    private NotificationManager mNotificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//
//        //绑定Service
//        Intent intent = new Intent("com.example.communication.MSG_ACTION");
//        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    /**
     * 发送消息
     * @param view
     */
    public void onClick(View view) {
        Intent bind = new Intent(this, HintService.class);
        bindService(bind, connec, Context.BIND_AUTO_CREATE);
    }

    /**
     * 发送消息
     * @param view
     */
    public void onSend(View view) {
        mNotificationManager.notify(Context.BIND_AUTO_CREATE, getNotification());
    }


    ServiceConnection connec = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (HintService.DeviceIBinder) service;
            // 活动里调用/连接服务
            binder.getBinderData();
//            binder.getBinderData2();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 判断，没有注册，就不取消注册
        if (connec != null){
            unbindService(connec);
        }
      /*  if (intent != null){
            stopService(intent);
        }*/
    }

    /**
     * 初始化通知栏
     */
    private Notification getNotification() {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this,0, new Intent[]{intent},0);
        Notification.Builder builder = new Notification.Builder(this)
                // 设置小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                // 设置小图标旁的文本信息
                .setContentInfo("2")
                // 设置状态栏文本标题
                .setTicker("你的系统有更新")
                // 设置标题
                .setContentTitle("系统更新")
                // 设置内容
                .setContentText("发现系统更新，点击查看详情" + DateUtils.getTimeAccurate())
                // 设置ContentIntent
                .setContentIntent(pendingIntent)
                // 设置Notification提示铃声为系统默认铃声
                .setSound(RingtoneManager.getActualDefaultRingtoneUri(
                        getBaseContext(), RingtoneManager.TYPE_NOTIFICATION))
                // 点击Notification的时候自动移除
                .setAutoCancel(true);

        //设置Notification的ChannelID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("1");
        }
        Notification mNotification = builder.build();
        /*if (Build.VERSION.SDK_INT < 14) {
            mNotification.contentView = customView;
        }*/
        return mNotification;
    }

}
